package com.zx.controller;


import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zx.entity.EHetong;
import com.zx.entity.EHetongdetail;
import com.zx.entity.HetongAll;
import com.zx.entity.ref.ResponseResult;
import com.zx.service.IEHetongService;
import com.zx.service.IEHetongdetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhouxin
 * @since 2020-06-10
 */
@CrossOrigin
@RestController
@RequestMapping("hetong")
public class EHetongController {


    @Autowired
    private IEHetongService hetongService;

    @Autowired
    private IEHetongdetailService hetongdetailService;

    /*
    * 销售的列表查询
    * */
    @PostMapping("getRoleList/{uid}")
    public ResponseResult getRoleList(@RequestBody EHetong hetong,@PathVariable("uid") Long uid){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            if (hetong.getHetongname()!=null){
                queryWrapper.like("hetongname",hetong.getHetongname());
            }
            if (uid!=null){
                queryWrapper.eq("uid",uid);
            }
            queryWrapper.orderByDesc("creattime");
            //这个是普通销售的记录（只允许个人的数据）
            List list = hetongService.list(queryWrapper);
            result.setResult(list);
            result.setMessage("查询成功!");
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("查询失败!");
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping("addHetong/{uid}")
    public ResponseResult addHetong(@RequestBody HetongAll hetongAll,@PathVariable("uid") Integer uid){
        hetongAll.getHetong().setUid(uid);
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            hetongService.addAll(hetongAll);
            result.setMessage("添加成功!");
        }catch (Exception e){
            result.setMessage("添加失败!");
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping("randomid")
    public ResponseResult  randomid(){
        ResponseResult result = ResponseResult.SUCCESS();
        String randomids="";
        for (int i=0;i<6;i++){
            randomids+=new Random().nextInt(10);
        }
        result.setResult(randomids);
        return result;
    }

    @PostMapping("UpdateStatus/{id}")
    public ResponseResult UpdateStatus(@PathVariable("id") Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            EHetong hetong = hetongService.getById(id);
            hetong.setStatus("1");
            hetongService.saveOrUpdate(hetong);
            result.setMessage("修改成功!");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("修改失败！");
        }
        return result;
    }

    @PostMapping("lookdetail/{id}")
    public ResponseResult lookdetail(@PathVariable("id") Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            EHetong hetong = hetongService.getById(id);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("hid",id);
            List list = hetongdetailService.list(queryWrapper);
            HetongAll hetongAll = new HetongAll();
            hetongAll.setHetong(hetong);
            hetongAll.setHetongdetailList(list);
            result.setMessage("查询成功!");
            result.setResult(hetongAll);
        }catch (Exception e){
            result.setMessage("查询失败!");
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping("searchAll")
    public ResponseResult searchAll(@RequestBody EHetong hetong){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            if (hetong.getHetongname()!=null){
                queryWrapper.like("hetongname",hetong.getHetongname());
            }
            queryWrapper.between("status",1,3);
            queryWrapper.orderByDesc("creattime");
            //这个是普通销售的记录（只允许个人的数据）
            List list = hetongService.list(queryWrapper);
            result.setResult(list);
            result.setMessage("查询成功!");
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("查询失败!");
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 用于通过审核
     */
    @PostMapping("GoOk/{id}")
    public ResponseResult GoOk(@PathVariable("id") Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            EHetong hetong = hetongService.getById(id);
            hetong.setStatus("2");
            hetongService.saveOrUpdate(hetong);
            result.setMessage("修改成功!");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("修改失败！");
        }
        return result;
    }

    @PostMapping("GoNo/{id}")
    public ResponseResult GoNo(@PathVariable("id") Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            EHetong hetong = hetongService.getById(id);
            hetong.setStatus("3");
            hetongService.saveOrUpdate(hetong);
            result.setMessage("修改成功!");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("修改失败！");
        }
        return result;
    }

    @PostMapping("getAllPrice")
    public ResponseResult getAllPrice(){
        ResponseResult result = ResponseResult.SUCCESS();
        int priceall=0;
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("status",2);
            List<EHetong> list = hetongService.list(queryWrapper);
            for (EHetong hetong:list) {
                priceall += new Integer(hetong.getMoney());
            }
            System.out.println(priceall);
            result.setResult(priceall);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("失败！");
        }
        return result;
    }

    /*@PostMapping("addpriceall")
    public ResponseResult addpriceall(@RequestBody HetongAll hetongAll){
        ResponseResult result = ResponseResult.SUCCESS();
        BigDecimal all=null;
        try {
            List<EHetongdetail> hetongdetailList = hetongAll.getHetongdetailList();
            for (EHetongdetail hetongdetail:hetongdetailList) {
                BigDecimal price = hetongdetail.getPrice();
            }
            result.setResult(price);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }*/

}
